package com.yhsimon.controller;



import com.yhsimon.entity.AdviceVO;
import com.yhsimon.entity.User;
import com.yhsimon.entity.UserVO;
import com.yhsimon.repository.UserRepository;
import com.yhsimon.util.MySecurity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/user")
public class UserHandler {

    @Autowired
    private UserRepository userRepository;

    @Value(value = "${server.port}")
    private String port;

    @GetMapping("/index")
    public String index() {
        return "端口号：" + this.port;
    }


    @GetMapping("/findAll/{page}/{limit}")
    public UserVO findAll(@PathVariable("page") int page, @PathVariable("limit") int limit) {
        UserVO userVO = new UserVO();
        userVO.setCode(0);
        userVO.setMsg("");
        userVO.setCount(userRepository.count());
        int index = (page - 1) * limit;
        userVO.setData(userRepository.findAll(index, limit));
        return userVO;
    }

    @GetMapping("/findById/{id}")
    public User findAll(@PathVariable("id") int id) {
        User user = userRepository.findById(id);
        if(user!=null){
            user.setPassword("");
        }
        return user;
    }

    @GetMapping("/findByName/{userName}")
    public User findByName(@PathVariable("userName") String userName){
        User user = userRepository.findByName(userName);
        if(user!=null){
            user.setPassword("");
        }
        return user;
    }

    @PostMapping("/save")
    public void save(@RequestBody User user) {
        user.setRegisterDate(new Date());
        user.setPassword(MySecurity.encryptUserPassword(user.getPassword()));
        userRepository.save(user);
    }

    @PostMapping("/advice/{id}/{advice}")
    public void advice(@PathVariable("id")int id,@PathVariable("advice") String advice) {
        User user = userRepository.findById(id);
        System.out.println("advice==="+advice+"-----"+user.getUserName());
        userRepository.advice(user.getUserName(),advice);

    }

    @GetMapping("/findAllAdvice/{page}/{limit}")
    public AdviceVO findAllAdvice(@PathVariable("page") int page, @PathVariable("limit") int limit) {
        AdviceVO adviceVO = new AdviceVO();
        adviceVO.setCode(0);
        adviceVO.setMsg("");
        adviceVO.setCount(userRepository.count2());
        int index = (page - 1) * limit;
        adviceVO.setData(userRepository.findAllAdvice(index, limit));
        return adviceVO;
    }

    @PutMapping("/update")
    public void update(@RequestBody User user) {
        user.setPassword(MySecurity.encryptUserPassword(user.getPassword()));
        userRepository.update(user);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") int id) {
        userRepository.deleteById(id);
    }
    @DeleteMapping("/deleteAdviceById/{id}")
    public void deleteAdviceById(@PathVariable("id") int id) {
        userRepository.deleteAdviceById(id);
    }

}
